package com.aplink.generic.xmlparser;

import android.content.Context;

import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.pojo.xml.ColumnSetting;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

public class ColumnSettingParser {

    private static String getTextNodeValue( Node node ) {
        Node child;
        if ( node != null ) {
            if ( node.hasChildNodes() ) {
                child = node.getFirstChild();
                while ( child != null ) {
                    if ( child.getNodeType() == Node.TEXT_NODE ) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return "";
    }

    public static ArrayList<ColumnSetting> parse( Document doc, String tagname ) {

        final ArrayList<ColumnSetting> columnSettingList = new ArrayList<ColumnSetting>();
        final NodeList nList = doc.getElementsByTagName( tagname );
        for ( int temp = 0 ; temp < nList.getLength() ; temp++ ) {

            final Node nNode = nList.item( temp );
            final ColumnSetting columnsetting = new ColumnSetting();
            // set column type
            if ( nNode.hasAttributes() ) {
                Attr attr = (Attr) nNode.getAttributes().getNamedItem(
                        ConfigParameter.COL_TYPE );
                if ( attr != null ) {
                    final String colType = attr.getNodeValue();
                    columnsetting.setColType( colType );
                }
                attr = (Attr) nNode.getAttributes().getNamedItem(
                        ConfigParameter.COL_FORMAT );
                if ( attr != null ) {
                    final String colFormat = attr.getNodeValue();
                    columnsetting.setFormat( colFormat );
                }
            }

            if ( nNode.getNodeType() == Node.ELEMENT_NODE ) {
                final Element element = (Element) nNode;

                // set mapping name
                NodeList nodes = element
                        .getElementsByTagName( ConfigParameter.COL_MAPPINGNAME );
                if ( nodes.getLength() != 0 ) {
                    final Node node = nodes.item( 0 );
                    columnsetting.setMappingName( getTextNodeValue( node ) );
                }

                // set linking mapping name
                nodes = element
                        .getElementsByTagName( ConfigParameter.COL_LINKINGMAPPINGNAME );
                if ( nodes.getLength() != 0 ) {
                    columnsetting.setLinkMappingName( getTextNodeValue( nodes
                            .item( 0 ) ) );
                }

                // set linkpage
                nodes = element
                        .getElementsByTagName( ConfigParameter.COL_LINKPAGE );
                if ( nodes.getLength() != 0 ) {
                    columnsetting.setLinkPage( getTextNodeValue( nodes.item( 0 ) ) );
                }

                // set header
                nodes = element
                        .getElementsByTagName( ConfigParameter.COL_HEADER );
                if ( nodes.getLength() != 0 ) {
                    columnsetting
                            .setHeaderText( getTextNodeValue( nodes.item( 0 ) ) );
                }
            }
            columnSettingList.add( columnsetting );
        }
        return columnSettingList;
    }

    public static ArrayList<ColumnSetting> parserIntoColumnSetting(
            Context paramContext, int xmlFileId, String tagname ) {
        Document document;
        try {
            document = XMLPaserUtil.openDocumentFrom( paramContext, xmlFileId );
            return parse( document, tagname );
        } catch ( final ParserConfigurationException e ) {
            e.printStackTrace();
        } catch ( final SAXException e ) {
            e.printStackTrace();
        } catch ( final IOException e ) {
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<ColumnSetting> parserIntoColumnSetting(
            Context paramContext, String xmlfilename, String tagname ) {

        try {
            final Document document = XMLPaserUtil.openDocumentFrom(
                    paramContext, xmlfilename );
            return parse( document, tagname );
        } catch ( final ParserConfigurationException e ) {
            e.printStackTrace();
        } catch ( final SAXException e ) {
            e.printStackTrace();
        } catch ( final IOException e ) {
            e.printStackTrace();
        }
        return null;
    }
}
